<template>
  <div class="report-operationrenew-container">
    <vab-query-form>
      <vab-query-form-left-panel style="padding-top: 6px">
        <el-descriptions title="运营部新签/续签明细" :column="3" border />
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space>
            <el-form-item label="请选择查询月份">
              <el-date-picker
                v-model="queryForm.month"
                type="month"
                placeholder="请选择月份"
                style="width: 120px"
              />
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryForm.city" style="width: 130px">
                <el-option :value="0" label="请选择区域" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.title"
                clearable
                placeholder="可输入想要查询的关键字"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-tabs type="border-card">
      <el-tab-pane label="续签">
        <el-table :header-cell-style="{ background: '#FFFFCC', color: '#000' }">
          <el-table-column label="赠送内容" />
          <el-table-column label="到账人" />
          <el-table-column label="主管组" />
          <el-table-column label="签单组提点" width="120" />
          <el-table-column label="小组提成" />
          <el-table-column label="原业务员所属部门" width="150" />
          <el-table-column label="原业务员工号" width="150" />
          <el-table-column label="原业务员" width="150" />
          <el-table-column label="原业务员提点" />
          <el-table-column label="原业务员提成" width="150" />
          <el-table-column label="备注" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="新签">
        <el-table :header-cell-style="{ background: '#FFFFCC', color: '#000' }">
          <el-table-column label="新签店铺基础信息" align="center">
            <el-table-column label="序号" align="center" width="60" />
            <el-table-column label="到账时间" align="center" width="100" />
            <el-table-column label="签约时间" align="center" width="100" />
            <el-table-column label="托管到期时间" align="center" width="130" />
            <el-table-column label="新签年限" align="center" width="100" />
            <el-table-column label="客户名称" align="center" width="100" />
            <el-table-column label="性质（备注）" align="center" width="130" />
            <el-table-column label="到账金额" align="center" width="100" />
            <el-table-column label="扣成本金额" align="center" width="100" />
            <el-table-column label="核算金额" align="center" width="100" />
            <el-table-column label="赠送内容" align="center" width="100" />
            <el-table-column
              label="店长组所属地区"
              align="center"
              width="150"
            />
            <el-table-column label="签单小组单量" align="center" width="150" />
            <el-table-column label="到账人单量" align="center" width="140" />
            <el-table-column label="签单小组" align="center" width="100" />
            <el-table-column label="到账人" align="center" width="100" />
            <el-table-column label="主管组" align="center" width="100" />
            <el-table-column label="到账人" align="center" width="100" />
            <el-table-column label="签单组提点" align="center" width="100" />
            <el-table-column label="小组提成" align="center" width="100" />
            <el-table-column label="备注" align="center" width="100" />
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/reportOperationrenew'
  import Edit from './components/ReportOperationrenewEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'ReportOperationrenew',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
